package com.ttms.service.impl;


import com.ttms.dao.impl.UserDaoImpl;
import com.ttms.pojo.Page;
import com.ttms.pojo.Play;
import com.ttms.pojo.User;
import com.ttms.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {

    private UserDaoImpl userDao = new UserDaoImpl();

    @Override
    public void registUser(User user) {

        userDao.savaUser(user);
    }

    @Override
    public User login(User user) {
        return userDao.queryUserByUsernameAndPassword(user.getPhone(), user.getPassword());
    }

    @Override
    public boolean existsUsername(String username) {
        boolean b = userDao.queryUserByUsername(username) == null;
        return !b;
    }

    @Override
    public Page<User> userList(int pageNo, int pageSize ) {
        Page<User> page = new Page<User>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = userDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<User> items = userDao.queryForPageItems(begin, pageSize);
        page.setItems(items);
        return page;
    }

    @Override
    public Page<User> page(int pageNo, int pageSize) {
        Page<User> page = new Page<User>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = userDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<User> items = userDao.queryForPageItems(begin, pageSize);
        page.setItems(items);
        return page;
    }

    @Override
    public User queryUserById(Integer id) {
        return userDao.queryUserById(id);
    }

    @Override
    public User queryUserByPhone(String phone) {
        return userDao.queryUserByPhone(phone);
    }

    @Override
    public int resetPassword(String password,int id) {
        return userDao.resetPassWord(password,id);
    }

    @Override
    public int rename(String phone, String name, int id) {
        return userDao.rename( phone, name,id);
    }

    @Override
    public int resetHead(String head_path) {
        return userDao.resetHead(head_path);
    }

}
